package com.ruoyi.wms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.dto.InventoryItemDto;
import com.ruoyi.wms.mapper.StockInventoryMapper;
import com.ruoyi.wms.vo.InventoryItemVo;
import com.ruoyi.wms.vo.StockInventoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.InventoryItemMapper;
import com.ruoyi.wms.domain.InventoryItem;
import com.ruoyi.wms.service.IInventoryItemService;

/**
 * 盘点明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-19
 */
@Service
public class InventoryItemServiceImpl implements IInventoryItemService {
    @Autowired
    private InventoryItemMapper inventoryItemMapper;
    @Autowired
    private StockInventoryMapper stockInventoryMapper;
    /**
     * 查询盘点明细
     *
     * @param id 盘点明细主键
     * @return 盘点明细
     */
    @Override
    public InventoryItem selectInventoryItemById(String id) {
        return inventoryItemMapper.selectInventoryItemById(id);
    }

    /**
     * 查询盘点明细列表
     *
     * @param inventoryItem 盘点明细
     * @return 盘点明细
     */
    @Override
    public List<InventoryItemVo> selectInventoryItemList(InventoryItemDto inventoryItem) {
        return inventoryItemMapper.selectInventoryItemList(inventoryItem);
    }

    /**
     * 新增盘点明细
     *
     * @param inventoryItem 盘点明细
     * @return 结果
     */
    @Override
    public int insertInventoryItem(InventoryItem inventoryItem) {
        inventoryItem.setId(MD5Utils.md5(inventoryItem.getBatchNum()+inventoryItem.getProductName()));
        inventoryItem.setCreateUser(SecurityUtils.getUsername());
        inventoryItem.setCreateDate(DateUtils.getCurrentTime());
        return inventoryItemMapper.insertInventoryItem(inventoryItem);
    }

    /**
     * 修改盘点明细
     *
     * @param inventoryItem 盘点明细
     * @return 结果
     */
    @Override
    public int updateInventoryItem(InventoryItem inventoryItem) {

        int updateResult = 0;
        if (StringUtils.isEmpty(inventoryItem.getId())) {
            updateResult = insertInventoryItem(inventoryItem);
        }
        updateResult =inventoryItemMapper.updateInventoryItem(inventoryItem);
        if (0 == updateResult) {
            updateResult = insertInventoryItem(inventoryItem);
        }


        return updateResult;




    }

    /**
     * 批量删除盘点明细
     *
     * @param ids 需要删除的盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteInventoryItemByIds(String[] ids) {
        return inventoryItemMapper.deleteInventoryItemByIds(ids);
    }

    /**
     * 删除盘点明细信息
     *
     * @param id 盘点明细主键
     * @return 结果
     */
    @Override
    public int deleteInventoryItemById(String id) {
        return inventoryItemMapper.deleteInventoryItemById(id);
    }

    @Override
    public InventoryItem getInfoInventoryItemInfo(InventoryItemDto dto) {
        return inventoryItemMapper.getInfoInventoryItemInfo(dto);
    }

    @Override
    public  List<HashMap<String, Object>> save(InventoryItem inventoryItem) {
        updateInventoryItem(inventoryItem);
        InventoryItemDto dto =new InventoryItemDto();
        dto.setBatchNum( inventoryItem.getBatchNum());
        dto.setStatus(true);
//      查询剩余未盘点数量
        List<HashMap<String, Object>>  result= inventoryItemMapper.queryNotInventoryItemCnt (dto);
        return  result;
    }

    @Override
    public StockInventoryVo queryInventoryItemResult(String batchNum) {
        StockInventoryVo vo= stockInventoryMapper.selectStockInventoryByBatchNum(batchNum );
        List<Map<String, Object>> greater = inventoryItemMapper.queryPdaInventoryItem(batchNum,"greater");
        List<Map<String, Object>> less = inventoryItemMapper.queryPdaInventoryItem(batchNum,"less");
        List<Map<String, Object>> equal = inventoryItemMapper.queryPdaInventoryItem(batchNum,"equal");

        vo.setNotCount(  inventoryItemMapper.queryNotCount(batchNum) );
        vo.setGreater( greater);
        vo.setLess( less);
        vo.setEqual( equal);
        return  vo;
    }

    @Override
    public int queryNotInventoryItemCnt(String batchNum) {
        return  inventoryItemMapper.queryNotCount(batchNum);
    }
}
